<style type="text/css">
    #hr-trainningBatch-update {
        padding: 20px 25px 25px 0;
    }
</style>

<div class="layui-fluid" id="hr-trainningBatch-update">
    <form class="layui-form" action="" lay-filter="hr-trainningBatch-update-form">
        <!-- 隐藏当前对象id-->
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">批次id：</label>
            <div class="layui-input-block">
                <input type="text" name="id" data-th-value="${trainningBatch.id}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">批次名称</label>
            <div class="layui-input-block">
                <input type="text" name="trainningName" minlength="2" maxlength="20" lay-verify="char|required|range|name|character"
                       autocomplete="off" class="layui-input repetitive" id="hr-trainningBatch-add-name">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" maxlength="200" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="trainningBatch-update-form-submit"
                    id="hr-trainningBatch-update-submit-btn"></button>
        </div>
    </form>
</div>

<script data-th-inline="javascript">
    layui.use(['febs', 'form', 'validate'], function () {
        let $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            // 获得后台出入的实体对象
            trainningBatch = [[${trainningBatch}]],
            // 验证
            validate = layui.validate;
        // 表单验证
        form.verify(validate);
        // 表单渲染
        form.render();
        // 初始化页面数据
        initCollegeValue();
        // 初始化页面数据
        function initCollegeValue() {
            form.val("hr-trainningBatch-update-form", {
                "id": trainningBatch.id,
                "trainningName": trainningBatch.trainningName,
                "remark": trainningBatch.remark
            });
        }

        // 表单提交事件
        form.on('submit(trainningBatch-update-form-submit)', function (data) {
            console.log(data.field)
            console.log(trainningBatch)
            console.log(febs.nativeEqual(data.field.id, trainningBatch.id)
                && febs.nativeEqual(data.field.trainningName, trainningBatch.trainningName))
            // 如果都没有修改直接返回
            if (febs.nativeEqual(data.field, trainningBatch)) {
                febs.alert.warn('数据未作任何修改！');
                return false;
                // 如果只修改了名字之外的东西，则直接更新
            }else if(febs.nativeEqual(data.field.id, trainningBatch.id)
                && febs.nativeEqual(data.field.trainningName, trainningBatch.trainningName)){
                febs.post(ctx + 'trainningBatch/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('培训批次修改成功');
                    // 点击父页面查询按钮
                    $('#hr-trainningBatch').find('#hr-trainningBatch-query-btn').click();
                });
                // 如果名字发生变化先进行判断是否重名，如果重名则返回，不重名则修改
            }else {
                let trainningName = data.field.trainningName;
                if (febs.nativeEqual(data.field.trainningName, trainningBatch.trainningName)) {
                    trainningName = null;
                }
                if (febs.nativeEqual(data.field.id, trainningBatch.id)) {
                    febs.alert.error("id为不可编辑列");
                    return false;
                }
                $.ajax({
                    type:'get',
                    url:'trainningBatch/isRepetitive',
                    data:{"trainningName":trainningName},
                    success:function(res){
                        if(res){
                            febs.post(ctx + 'trainningBatch/update', data.field, function () {
                                layer.closeAll();
                                febs.alert.success('院校数据修改成功');
                                $('#hr-trainningBatch').find('#hr-trainningBatch-query-btn').click();
                            });
                        }else {
                            febs.alert.error("输入的名称重复，请检查");
                        }

                    }
                });
            }
            return false;
        });
    });
</script>